#include "figures.h"
#include "colors.h"
#include "material.h"
#include "texture.h"
#include "wall.h"

#define AXIS_X 'x'
#define AXIS_Y 'y'
#define AXIS_Z 'z'

float gunAngle=0;
float angleCocaCola=0;
GLfloat eye[3];

struct door_t door1; 
struct door_t door2; 
struct door_t door3; 
struct door_t door4; 


void disegnaCornice(GLfloat x,GLfloat y,GLfloat z, GLfloat w, GLfloat h,int texture, GLfloat rotationAngle, char axis) {
	materials(&marroneOpaco);
	glPushMatrix();
		glTranslatef(x,y,z);
			if (axis=='x'){
					glRotatef(rotationAngle,1,0,0);
			}else if (axis=='y'){
					glRotatef(rotationAngle,0,1,0);
			}else if (axis=='z'){
					glRotatef(rotationAngle,0,0,1);
			}
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0); 	glVertex3f(0, 0.1, 0);
				glTexCoord2f(4, 0); 	glVertex3f(4, 0.1, 0);
				glTexCoord2f(4, 0.1); 	glVertex3f(4, 0.1, 0.1);
				glTexCoord2f(0, 0.1); 	glVertex3f(0, 0.1, 0.1);
				
				glTexCoord2f(0.1, 0); 	glVertex3f(0, 0.1, 0);
				glTexCoord2f(0.1, 0.1); 	glVertex3f(0, 0, 0);
				glTexCoord2f(0, 0.1); 	glVertex3f(0, 0, 0.1);
				glTexCoord2f(0, 0); 	glVertex3f(0, 0.1, 0.1);
				
				glTexCoord2f(0.1, 0.1); 	glVertex3f(4, 0.1, 0.1);
				glTexCoord2f(0, 0.1); 	glVertex3f(4, 0, 0.1);
				glTexCoord2f(0, 0); 	glVertex3f(4, 0, 0);
				glTexCoord2f(0.1, 0); 	glVertex3f(0.10, 0.1, 0);
				
				glTexCoord2f(0, 0.1); 	glVertex3f(0, 0.1, 0);
				glTexCoord2f(0, 0); 	glVertex3f(0, 0, 0);
				glTexCoord2f(4, 0); 	glVertex3f(4, 0, 0);
				glTexCoord2f(4, 0.1); 	glVertex3f(4, 0.1, 0);
		glEnd();
	glPopMatrix();
}

void disegnaPoster(GLfloat x,GLfloat y,GLfloat z, GLfloat w, GLfloat h,int texture, GLfloat rotationAngle, char axis) {
	//disegna un poster w x h
	applyTexture(texture);
	materials(&biancoOpaco);
	glPushMatrix();
		glTranslatef(x, y, z);
			if (axis=='x'){
					glRotatef(rotationAngle,1,0,0);
			}else if (axis=='y'){
					glRotatef(rotationAngle,0,1,0);
			}else if (axis=='z'){
					glRotatef(rotationAngle,0,0,1);
			}
			glPushMatrix();
				glBegin(GL_QUADS);
					glTexCoord2f(0, 0);		glVertex3f(0, 0, 0);
					glTexCoord2f(-1, 0);	glVertex3f(w, 0, 0);
					glTexCoord2f(-1, -1);	glVertex3f(w, 0, h);
					glTexCoord2f(0, -1);	glVertex3f(0, 0, h);
				//disegnaCornice(0,0,-0.1);
				glEnd();
			glPopMatrix();
	glPopMatrix();
}

void disegnaPannelloPubblicitario(GLfloat position[3], GLfloat p1[2], GLfloat p2[2], int texture, float angle) {
	materials(&biancoEmittivo);
	glPushMatrix();
		glTranslatef(position[0], position[1], position[2]);
		glPushMatrix();
			glRotatef(angle, 0, 0, 1);
			applyTexture(TX_CORNICE);
				glPushMatrix();	//posizionamento dei sostegni
					glTranslatef( (p2[0]-p1[0])/2-(p2[0]-p1[0])*0.1, 0, 0);
					drawCilinder(1.5, 0.1, 4);
				glPopMatrix();
				glPushMatrix();
					glTranslatef(-((p2[0]-p1[0])/2-(p2[0]-p1[0])*0.1), 0, 0);
					drawCilinder(1.5, 0.1, 4);
				glPopMatrix();
			applyTexture(texture);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);		glVertex3f(-(p2[0]-p1[0])/2, 0, 1.5);
				glTexCoord2f(-1, 0);	glVertex3f( (p2[0]-p1[0])/2, 0, 1.5);
				glTexCoord2f(-1, -1);	glVertex3f( (p2[0]-p1[0])/2, 0, 1.5+p1[1]-p2[1]);
				glTexCoord2f(0, -1);	glVertex3f(-(p2[0]-p1[0])/2, 0, 1.5+p1[1]-p2[1]);
			glEnd();
		glPopMatrix();
	glPopMatrix();
}

void drawCilinder(float height, float radius, int lati) {
	/*
	GLfloat red[] = {1,0,0,1};
	GLfloat brown[] = {0.4,0,0,1};
	GLfloat green[] = {0,1,0,1};
	GLfloat blue[] = {0,0,1,1};
	GLfloat white[] = {1,1,1,1};
	GLfloat black[] = {0,0,0,1};
	*/
	float angle;
	int i;
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glBegin(GL_QUADS);
		angle = 2.0*3.14/lati;
		for(i=0; i<lati; i++) {
			glNormal3f(-cos((i*angle+(i+1)*angle)/2),-sin((i*angle+(i+1)*angle)/2),0.0);
			glTexCoord2f(radius*cos(i*angle), 0);	glVertex3f(radius*cos(i*angle), radius*sin(i*angle),0.0);
			glTexCoord2f(radius*cos(i*angle), height);	glVertex3f(radius*cos(i*angle), radius*sin(i*angle),height);
			glTexCoord2f(radius*cos((i+1)*angle), height);	glVertex3f(radius*cos((i+1)*angle), radius*sin((i+1)*angle),height);
			glTexCoord2f(radius*cos((i+1)*angle), 0);	glVertex3f(radius*cos((i+1)*angle), radius*sin((i+1)*angle),0.0);
		}
	glEnd();
	glBegin(GL_POLYGON);
		for(i=0; i<lati; i++) {
			glNormal3f(0,0,1);
			//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT, brown);
			glTexCoord2f(radius*cos(i*angle), radius*sin(i*angle));
			glVertex3f(radius*cos(i*angle), radius*sin(i*angle),0);
		}
	glEnd();
	glBegin(GL_POLYGON);
		for(i=0; i<lati; i++) {
			glNormal3f(0,0,1);
			//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT, brown);
			glTexCoord2f(radius*cos(i*angle), radius*sin(i*angle));
			glVertex3f(radius*cos(i*angle), radius*sin(i*angle),height);
		}
	glEnd();
}

void drawPavimento(int texture, int stanza) {
	/*PAVIMENTO*/
	//glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture);
	switch (stanza){
		case 1:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(45,80,0);
					glTexCoord2f(0,20); glVertex3f(85,80,0);
					glTexCoord2f(20,20); glVertex3f(85,40,0);
					glTexCoord2f(20,0); glVertex3f(45,40,0);
				glEnd();
			glPopMatrix();
			break;
		case 2:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(105,75,0);
					glTexCoord2f(0,20); glVertex3f(120,75,0);
					glTexCoord2f(20,20); glVertex3f(120,45,0);
					glTexCoord2f(20,0); glVertex3f(105,45,0);
				glEnd();
			glPopMatrix();
			break;
		case 3:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(50,115,0);
					glTexCoord2f(0,20); glVertex3f(80,115,0);
					glTexCoord2f(20,20); glVertex3f(80,100,0);
					glTexCoord2f(20,0); glVertex3f(50,100,0);
				glEnd();
			glPopMatrix();
			break;
		case 4:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(10,75,0);
					glTexCoord2f(0,20); glVertex3f(25,75,0);
					glTexCoord2f(20,20); glVertex3f(25,45,0);
					glTexCoord2f(20,0); glVertex3f(10,45,0);
				glEnd();
			glPopMatrix();
			break;
		case 5:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(50,20,0);
					glTexCoord2f(0,20); glVertex3f(80,20,0);
					glTexCoord2f(20,20); glVertex3f(80,5,0);
					glTexCoord2f(20,0); glVertex3f(50,5,0);
				glEnd();
			glPopMatrix();
			break;
		case 11:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(85,65,0);
					glTexCoord2f(0,20); glVertex3f(105,65,0);
					glTexCoord2f(20,20); glVertex3f(105,55,0);
					glTexCoord2f(20,0); glVertex3f(85,55,0);
				glEnd();
			glPopMatrix();
			break;
		case 12:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(60,100,0);
					glTexCoord2f(0,20); glVertex3f(70,100,0);
					glTexCoord2f(20,20); glVertex3f(70,80,0);
					glTexCoord2f(20,0); glVertex3f(60,80,0);
				glEnd();
			glPopMatrix();
			break;
		case 13:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(25,65,0);
					glTexCoord2f(0,20); glVertex3f(45,65,0);
					glTexCoord2f(20,20); glVertex3f(45,55,0);
					glTexCoord2f(20,0); glVertex3f(25,55,0);
				glEnd();
			glPopMatrix();
			break;
		case 14:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(60,40,0);
					glTexCoord2f(0,20); glVertex3f(70,40,0);
					glTexCoord2f(20,20); glVertex3f(70,20,0);
					glTexCoord2f(20,0); glVertex3f(60,20,0);
				glEnd();
			glPopMatrix();
			break;
	}
}

void drawSoffitto(int texture, int stanza) {
	/*SOFFITTO*/
	//glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture);
	switch (stanza){
		case 1:
			glPushMatrix();
				materials(&grigioOpaco);
				glBegin(GL_QUADS);
				glNormal3f(0,0,-1);
					glTexCoord2f(0,0); glVertex3f(45,80,10);
					glTexCoord2f(0,20); glVertex3f(85,80,10);
					glTexCoord2f(20,20); glVertex3f(85,40,10);
					glTexCoord2f(20,0); glVertex3f(45,40,10);
				glEnd();
			glPopMatrix();
			break;
		case 2:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(105,75,10);
					glTexCoord2f(0,20); glVertex3f(120,75,10);
					glTexCoord2f(20,20); glVertex3f(120,45,10);
					glTexCoord2f(20,0); glVertex3f(105,45,10);
				glEnd();
			glPopMatrix();
			break;
		case 3:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(50,115,10);
					glTexCoord2f(0,20); glVertex3f(80,115,10);
					glTexCoord2f(20,20); glVertex3f(80,100,10);
					glTexCoord2f(20,0); glVertex3f(50,100,10);
				glEnd();
			glPopMatrix();
			break;
		case 4:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(10,75,10);
					glTexCoord2f(0,20); glVertex3f(25,75,10);
					glTexCoord2f(20,20); glVertex3f(25,45,10);
					glTexCoord2f(20,0); glVertex3f(10,45,10);
				glEnd();
			glPopMatrix();
			break;
		case 5:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(50,20,10);
					glTexCoord2f(0,20); glVertex3f(80,20,10);
					glTexCoord2f(20,20); glVertex3f(80,5,10);
					glTexCoord2f(20,0); glVertex3f(50,5,10);
				glEnd();
			glPopMatrix();
			break;
		case 11:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(85,65,5);
					glTexCoord2f(0,20); glVertex3f(105,65,5);
					glTexCoord2f(20,20); glVertex3f(105,55,5);
					glTexCoord2f(20,0); glVertex3f(85,55,5);
				glEnd();
			glPopMatrix();
			break;
		case 12:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(60,100,5);
					glTexCoord2f(0,20); glVertex3f(70,100,5);
					glTexCoord2f(20,20); glVertex3f(70,80,5);
					glTexCoord2f(20,0); glVertex3f(60,80,5);
				glEnd();
			glPopMatrix();
			break;
		case 13:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(25,65,5);
					glTexCoord2f(0,20); glVertex3f(45,65,5);
					glTexCoord2f(20,20); glVertex3f(45,55,5);
					glTexCoord2f(20,0); glVertex3f(25,55,5);
				glEnd();
			glPopMatrix();
			break;
		case 14:
			glPushMatrix();
				materials(&grigioOpaco);
				glNormal3f(0,0,-1);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0); glVertex3f(60,40,5);
					glTexCoord2f(0,20); glVertex3f(70,40,5);
					glTexCoord2f(20,20); glVertex3f(70,20,5);
					glTexCoord2f(20,0); glVertex3f(60,20,5);
				glEnd();
			glPopMatrix();
			break;
	}

}


void disegnaPavimentiSoffitti(){
	glPushMatrix();
		materials(&grigioOpaco);
		applyTexture(TX_PAVIMENTOEXT);
		glNormal3f(0,0,1);
		glBegin(GL_QUADS);
			glTexCoord2f(0,0);	glVertex3f(-100, -100, -0.5);
			glTexCoord2f(0,300);	glVertex3f(-100, 200, -0.5);
			glTexCoord2f(300,300);	glVertex3f(200, 200, -0.5);
			glTexCoord2f(300,0);	glVertex3f(200, -100, -0.5);
		glEnd();
	glPopMatrix();
	//pavimento stanze		
		drawPavimento(TX_FLOOR_S1,1);
		drawPavimento(TX_FLOOR_S2,2);
		drawPavimento(TX_FLOOR_S3,3);
		drawPavimento(TX_FLOOR_S4,4);
		drawPavimento(TX_FLOOR_S5,5);

//pavimento corridoi		

		drawPavimento(TX_CEILING_S1,11);
		drawPavimento(TX_CEILING_S1,12);
		drawPavimento(TX_CEILING_S1,13);
		drawPavimento(TX_CEILING_S1,14);

//soffitto stanze		
		drawSoffitto(TX_CEILING_S1,1);
		drawSoffitto(TX_CEILING_S2,2);
		drawSoffitto(TX_CEILING_S3,3);
		drawSoffitto(TX_CEILING_S4,4);
		drawSoffitto(TX_CEILING_S5,5);

//soffitto corridoi		

		drawSoffitto(TX_CEILING_S1,11);
		drawSoffitto(TX_CEILING_S1,12);
		drawSoffitto(TX_CEILING_S1,13);
		drawSoffitto(TX_CEILING_S1,14);

//		drawSoffitto(TX_CEILING,1);
	
}

void drawAxes() {
	GLfloat red[] = {1,0,0,1};
	GLfloat brown[] = {0.4,0,0,1};
	GLfloat green[] = {0,1,0,1};
	GLfloat blue[] = {0,0,1,1};
	GLfloat white[] = {1,1,1,1};
	GLfloat black[] = {0,0,0,1};
	glPushMatrix();
	glBegin(GL_LINES);
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, red);
	glVertex3f(0,0,0);glVertex3f(10,0,0);
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, green);
	glVertex3f(0,0,0);glVertex3f(0,10,0);
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, blue);
	glVertex3f(0,0,0);glVertex3f(0,0,10);
	glEnd();
	glPopMatrix();
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, white);
}

/**
 * ogni funzione disegnaFigura prende per parametri le coordinate dell'oggetto stesso
 * */
void disegnaTorretta(GLfloat x,GLfloat y,GLfloat z){
	/*definizione del muro alto 0 intorno alla torretta*/
	materials(&grigioOpaco);
	applyTexture(TX_CEILING_S1);
	glPushMatrix();
		glTranslatef(x,y,z);
			glPushMatrix();
				glRotatef(90,1,0,0);
				glScalef(4, 4, 4);
					display_ply(0,0);
			glPopMatrix();
	glPopMatrix();
}

void disegnaSamurai(GLfloat x,GLfloat y,GLfloat z, float angle){
	applyTexture(TX_TORRETTA_1);
	glPushMatrix();
		glTranslatef(x,y,2.5*z);	
			glPushMatrix();
			glRotatef(angle, 0, 0, 1);
				glPushMatrix();
					glRotatef(90,1,0,0);
						glScalef(2.5,2.5,2.5);
						display_ply(0,1);
				glPopMatrix();
			glPopMatrix();
	glPopMatrix();
}

void disegnaTesla(GLfloat x,GLfloat y,GLfloat z){
	applyTexture(TX_CEILING_S1);
	
	glPushMatrix();
//		open_ply("ply/samurai.ply");
		glTranslatef(x,y,2.5*z);	
			glPushMatrix();
	//			glRotatef(90,1,0,0);
					glScalef(2.5,2.5,2.5);
					display_ply(0,2);
			glPopMatrix();
	glPopMatrix();
}

void disegnaCocaCola(GLfloat x, GLfloat y, GLfloat z) {
			
	float width, height;
	int i, lati=10;
	float A=1, B=0.2;
	float angolo = 2*M_PI/lati;
	float normalAngle[3];
	glPushMatrix();
		glTranslatef(x, y, z);
			//glEnable(GL_TEXTURE_2D);
			materials(&biancoEmittivo);
			applyTexture(TX_CEILING_S1);
			drawCilinder(0.5, 0.1, 10);
			applyTexture(TX_COCACOLA);
			glRotatef((angleCocaCola+=0.5), 0, 0, 1);
			
			glBegin(GL_QUADS);
			/* le coordinate texture sono invertite in segno perche' la texture importata risulta invertita rispetto alle coordinate locali */
				for(i=0; i<lati; i++) {
					normalAngle[0] = ((i<lati/2)? -1:+1)*(A*cos(((i+1)*angolo-i*angolo)/2));
					normalAngle[1] = ((i<lati/2)? -1:+1)*(B*sin(((i+1)*angolo-i*angolo)/2));
					normalAngle[2] = 0;
					
					glNormal3f(normalAngle[0], normalAngle[1], normalAngle[2] );
					glTexCoord2f(-A*cos(i*angolo), 0);				glVertex3f(A*cos(i*angolo), B*sin(i*angolo), 0.5);
					glTexCoord2f(-A*cos(i*angolo), -1);		glVertex3f(A*cos(i*angolo), B*sin(i*angolo), 1.5);
					glTexCoord2f(-A*cos((i+1)*angolo), -1);	glVertex3f(A*cos((i+1)*angolo), B*sin((i+1)*angolo), 1.5);
					glTexCoord2f(-A*cos((i+1)*angolo), 0);		glVertex3f(A*cos((i+1)*angolo), B*sin((i+1)*angolo), 0.5);
				}
			glEnd();
			//glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}

void disegnaCilindro(float height, float radius, int lati) {
	float angle;
	int i;
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glBegin(GL_QUADS);
		angle = 2.0*3.14/lati;
		for(i=0; i<lati; i++) {
			glNormal3f(-cos((i*angle+(i+1)*angle)/2),-sin((i*angle+(i+1)*angle)/2),0.0);
			glTexCoord2f(radius*cos(i*angle), 0);	glVertex3f(radius*cos(i*angle), radius*sin(i*angle),0.0);
			glTexCoord2f(radius*cos(i*angle), height);	glVertex3f(radius*cos(i*angle), radius*sin(i*angle),height);
			glTexCoord2f(radius*cos((i+1)*angle), height);	glVertex3f(radius*cos((i+1)*angle), radius*sin((i+1)*angle),height);
			glTexCoord2f(radius*cos((i+1)*angle), 0);	glVertex3f(radius*cos((i+1)*angle), radius*sin((i+1)*angle),0.0);
		}
	glEnd();
	/*
	glBegin(GL_POLYGON);
		for(i=0; i<lati; i++) {
			glNormal3f(0,0,1);
			//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT, brown);
			glTexCoord2f(radius*cos(i*angle), radius*sin(i*angle));
			glVertex3f(radius*cos(i*angle), radius*sin(i*angle),0);
		}
	glEnd();
	*/
	glBegin(GL_POLYGON);
		for(i=0; i<lati; i++) {
			glNormal3f(0,0,1);
			glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT, black);
			glTexCoord2f(radius*cos(i*angle), radius*sin(i*angle));
			glVertex3f(radius*cos(i*angle), radius*sin(i*angle),height);
		}
	glEnd();
}

void disegnaCannone(GLfloat x, GLfloat y, GLfloat z) {
	float angle;
	float direction[3] = {eye[0]-x, eye[1]-y, eye[2]-z};
	/*normalizzo la direzione*/
	float normal = sqrt(direction[0]*direction[0]+direction[1]*direction[1]+direction[2]*direction[2]);
	direction[0] = direction[0]/normal;
	direction[1] = direction[1]/normal;
	direction[2] = direction[2]/normal;
	
	//angle = (x*eye[0]+y*eye[1] +z*eye[2])/(sqrt(x*x+y*y)*sqrt(eye[0]*eye[0]+eye[1]*eye[1]));
	//angle = (0*direction[0]+1*direction[1] +0*direction[2])/(sqrt(0*0+1*1)*sqrt(direction[0]*direction[0]+direction[1]*direction[1]));
	angle = (direction[1])/(sqrt(direction[0]*direction[0]+direction[1]*direction[1]));
	gunAngle = (x<65?-1:+1)*(acos(angle)*360)/(2*M_PI);
	
//	printf("Angolo: %f\n", gunAngle);
	
	glPushMatrix();
		glTranslatef(x, y, z);

		materials(&ottone);
		applyTexture(TX_TORRETTA_1);
		/*base della torretta*/
		disegnaCilindro(0.5, 0.7, 6);
		glPushMatrix();
			/*sostegno del cannone*/
			glTranslatef(0, 0, 0.5);
			disegnaCilindro(0.6, 0.3, 10);
		glPopMatrix();
		glPushMatrix();
			materials(&grigioOpaco);
			glTranslatef(0, 0, 1.1);
			disegnaCilindro(0.6, 0.6, 12);
		glPopMatrix();
		glPushMatrix();		
			/*la rotate deve ruotare il cannone verso il giocatore*/
			glRotatef(gunAngle, 0, 0, 1);	
			glPushMatrix();
				materials(&grigioOpaco);
				glTranslatef(0, 2, 1.5);
				glRotatef(90, 1, 0, 0);
				disegnaCilindro(1.8, 0.32, 18);
				glPushMatrix();
					glTranslatef(0, 0, -0.65);
					disegnaCilindro(0.7, 0.30, 18);
				glPopMatrix();
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
}

void disegnaPanca(GLfloat x, GLfloat y, GLfloat z, float angle) {
	glPushMatrix();
		glTranslatef(x, y, z);
		glRotatef(angle, 0,0,1);
		glPushMatrix();
			/*gambe della panca*/
			glPushMatrix();
				materials(&grigioOpaco);
				glPushMatrix();
					glTranslatef(-2, -0.5,0);
					glRotatef(45, 0, 0, 1);
					drawCilinder(0.3, 0.1, 4);
				glPopMatrix();
				glPushMatrix();
					glTranslatef(-2, 0.5,0);
					glRotatef(45, 0, 0, 1);
					drawCilinder(0.3, 0.1, 4);
				glPopMatrix();
				glPushMatrix();
					glTranslatef(2, 0.5,0);
					glRotatef(45, 0, 0, 1);
					drawCilinder(0.3, 0.1, 4);
				glPopMatrix();
				glPushMatrix();
					glTranslatef(2, -0.5,0);
					glRotatef(45, 0, 0, 1);
					drawCilinder(0.3, 0.1, 4);
				glPopMatrix();
			glPopMatrix();
			
			/*sedile della panca*/
			glPushMatrix();
				materials(&marroneOpaco);
				applyTexture(TX_CORNICE);
				glBegin(GL_QUADS);
				 	//piano
				 	glNormal3f(0,0,1);
					glTexCoord2f(0, 0);		glVertex3f(-2.1, -0.6, 0.4);
					glTexCoord2f(0, 2.4);	glVertex3f(-2.1, 0.6, 0.4);
					glTexCoord2f(2.4, 2.4);	glVertex3f(2.1, 0.6, 0.4);		
					glTexCoord2f(2.4, 0);	glVertex3f(2.1, -0.6, 0.4);
					//bordo sud
				 	glNormal3f(0,-1,0);
					glVertex3f(-2.1, -0.6, 0.4);	glVertex3f(-2.1, -0.6, 0.3);	glVertex3f(2.1, -0.6, 0.3);	glVertex3f(2.1, -0.6, 0.4);
					//bordo ovest
				 	glNormal3f(-1,0,0);
					glVertex3f(-2.1, -0.6, 0.4);	glVertex3f(-2.1, -0.6, 0.3);	glVertex3f(-2.1, 0.6, 0.3);	glVertex3f(-2.1, 0.6, 0.4);
					//bordo nord
				 	glNormal3f(0,1,0);
					glVertex3f(-2.1, 0.6, 0.4);	glVertex3f(-2.1, 0.6, 0.3);	glVertex3f(2.1, 0.6, 0.3);		glVertex3f(2.1, 0.6, 0.4);
					//bordo est
				 	glNormal3f(1,0,0);
					glVertex3f(2.1, 0.6, 0.4);		glVertex3f(2.1, 0.6, 0.3);		glVertex3f(2.1, -0.6, 0.3);	glVertex3f(2.1, -0.6, 0.4);
					
				glEnd();
			glPopMatrix();
			
		glPopMatrix();
	glPopMatrix();
}

void disegnaPilastro(GLfloat x, GLfloat y, GLfloat z) {
	glPushMatrix();
		glTranslatef(x, y, z);
		applyTexture(TX_TORRETTA_1);
		glPushMatrix();
			materials(&marroneScuroOpaco);
			drawCilinder(0.5, 1, 8);
			glPushMatrix();
				materials(&marroneOpaco);
				glTranslatef(0,0,0.5);
				drawCilinder(9, 0.6, 8);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0,0,9);
				materials(&marroneScuroOpaco);
				drawCilinder(0.5, 1, 8);
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
}

void disegnaPubblicitaAngolo(GLfloat x, GLfloat y, GLfloat z, int texture) {
	glPushMatrix();
		glTranslatef(x, y, z);
		materials(&biancoEmittivo);
		glPushMatrix();
			applyTexture(texture);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 1);			glVertex3f(-8, 0, 3);
				glTexCoord2f(0.5, 1);		glVertex3f(0, 0, 3);
				glTexCoord2f(0.5, 0);	glVertex3f(0, 0, 9);
				glTexCoord2f(0, 0);		glVertex3f(-8, 0, 9);

				glTexCoord2f(0.5, 1);	glVertex3f(0, 0, 3);
				glTexCoord2f(1, 1);		glVertex3f(0, 8, 3);
				glTexCoord2f(1, 0);		glVertex3f(0, 8, 9);
				glTexCoord2f(0.5, 0);		glVertex3f(0, 0, 9);
			glEnd();
		glPopMatrix();
	glPopMatrix();
}

/*per disegnare oggetti nelle stanze e nei corridoi*/
void drawObject(){
		GLfloat position[3], p1[2], p2[2];
		glPushMatrix();
					disegnaCannone(46,41,0);
					disegnaCannone(46,79,0);
					disegnaCannone(84,41,0);
					disegnaCannone(84,79,0);
					
					//disegnaQuadro(62.5, 40.1, 6);
					disegnaPoster(62.5,	 40.1, 	6,		4,		2.5,	TX_POSTER1,	0.0,		AXIS_X);
					disegnaPoster(50.5, 	79.8, 	2,		8,		6,		TX_POSTER2,	0.0,		AXIS_X);
					disegnaPoster(84.9,	51, 		2,		4,		6,		TX_POSTER3,	90.0,	AXIS_Z);
					disegnaPoster(119.9,	55, 		0,		10,		10,		TX_POSTER4,	90.0,	AXIS_Z);
					disegnaPoster(10.1,	64.5, 		0.5,		9,		4.2,		TX_POSTER5,	270.0,	AXIS_Z);
					disegnaPoster(79.9,	104, 		4,		6,		4.2,		TX_POSTER6,	90.0,	AXIS_Z);
					disegnaPoster(45.1,	50, 2, 6, 9.43,	TX_POSTER7,	90.0, AXIS_Z);
					disegnaPanca(30,64,0, 0);
					disegnaPanca(61,30,0, 90);
					
					disegnaCocaCola(23, 73, 0);
					disegnaCocaCola(11, 46, 0);
					
					position[0]=65;	position[1]=114;	position[2]=0;
					p1[0]=0;	p1[1]=4;	p2[0]=6;	p2[1]=0;
					disegnaPannelloPubblicitario(position, p1, p2, TX_PANNELLO1, 0);
					position[0]=51;	position[1]=107;	position[2]=0;
					p1[0]=0;	p1[1]=8;	p2[0]=5;	p2[1]=0;
					disegnaPannelloPubblicitario(position, p1, p2, TX_PANNELLO2, 90);
					position[0]=100;	position[1]=80;	position[2]=0;
					p1[0]=0;	p1[1]=5;	p2[0]=10;	p2[1]=0;
					disegnaPannelloPubblicitario(position, p1, p2, TX_DRAGO, -45);
					position[0]=35;	position[1]=90;	position[2]=0;
					p1[0]=0;	p1[1]=12;	p2[0]=8;	p2[1]=0;
					disegnaPannelloPubblicitario(position, p1, p2, TX_POSTER9, 90);
					position[0]=68.8;	position[1]=30;	position[2]=0;
					p1[0]=0;	p1[1]=6;	p2[0]=9.43;	p2[1]=0;
					disegnaPannelloPubblicitario(position, p1, p2, TX_POSTER8, 90);
					
					disegnaPubblicitaAngolo(81, 98, 0, TX_COCAPEPSI);
					
					disegnaPilastro(65, 60,0);	// centrale
					
					disegnaPilastro(71, 60,0);	// est
					disegnaPilastro(59, 60,0);	// ovest
					disegnaPilastro(65, 66,0);	// nord
					disegnaPilastro(65, 54,0);	// sud

					disegnaTorretta(54, 16,4);
					disegnaTorretta(54, 6,4);
					disegnaTorretta(79, 16,4);
					disegnaTorretta(79, 6,4);
					
					disegnaSamurai(60, 53, 1, -45);
					disegnaSamurai(70, 65, 1, 135);
					disegnaSamurai(58, 63, 1, -135);
					disegnaSamurai(72, 55, 1, 45);
					
		glPopMatrix();
}

void drawSolidParallelepiped(GLfloat startx,GLfloat starty,GLfloat endx,GLfloat endy,GLfloat heigth){
	glPushMatrix();
//	printf("Disegno il parallepipedo in posizione %f,%f alto %f\n",startx,starty,heigth);
		glBegin(GL_QUADS);
//bottom
			glVertex3f(startx,starty,0);
			glVertex3f(endx,starty,0);
			glVertex3f(endx,endy,0);
			glVertex3f(startx,endy,0);
//front
			glVertex3f(startx,starty,0);
			glVertex3f(endx,starty,0);
			glVertex3f(endx,starty,heigth);
			glVertex3f(startx,starty,heigth);
//up
			glVertex3f(startx,starty,heigth);
			glVertex3f(endx,starty,heigth);
			glVertex3f(endx,endy,heigth);
			glVertex3f(startx,endy,heigth);
//back
			glVertex3f(startx,endy,0);
			glVertex3f(endx,endy,0);
			glVertex3f(endx,endy,heigth);
			glVertex3f(startx,endy,heigth);
//right
			glVertex3f(endx,starty,0);
			glVertex3f(endx,endy,0);
			glVertex3f(endx,endy,heigth);
			glVertex3f(endx,starty,heigth);
//left
			glVertex3f(startx,starty,0);
			glVertex3f(startx,endy,0);
			glVertex3f(startx,endy,heigth);
			glVertex3f(startx,starty,heigth);
		glEnd();
	glPopMatrix();
}

void drawSolidDoor(GLfloat startx,GLfloat starty,GLfloat endx,GLfloat endy,GLfloat heigth, int tex, int orientation){
//	printf("SOLIDE DOOR TEXTURE %d\n",tex);
	materials(&grigioOpaco);
	glPushMatrix();
//			applyTexture(TX_DOORSIDE);
	//	printf("Disegno la porta in posizione %f,%f alto %f\n",startx,starty,heigth);
		glBegin(GL_QUADS);
//bottom
			glVertex3f(startx,starty,0);
			glVertex3f(endx,starty,0);
			glVertex3f(endx,endy,0);
			glVertex3f(startx,endy,0);
//front
		glEnd();
			if (orientation == VERT ){
				applyTexture(TX_DOORSIDE);
				glNormal3f(1.0,0.0,0.0);
			}else{
				applyTexture(tex);
				glNormal3f(0.0,1.0,0.0);
			}
		glBegin(GL_QUADS);
			glTexCoord2f(1,1);
			glVertex3f(startx,starty,0);
			glTexCoord2f(0,1);
			glVertex3f(endx,starty,0);
			glTexCoord2f(0,0);
			glVertex3f(endx,starty,heigth);
			glTexCoord2f(1,0);
			glVertex3f(startx,starty,heigth);
		glEnd();
		glBegin(GL_QUADS);
			
//up
			glVertex3f(startx,starty,heigth);
			glVertex3f(endx,starty,heigth);
			glVertex3f(endx,endy,heigth);
			glVertex3f(startx,endy,heigth);
//back
		glEnd();
			if (orientation == VERT ){
				applyTexture(TX_DOORSIDE);
				glNormal3f(-1.0,0.0,0.0);
			}
			else{
				applyTexture(tex);
				glNormal3f(0.0,-1.0,0.0);
			}
		glBegin(GL_QUADS);
			glTexCoord2f(1,1);
			glVertex3f(startx,endy,0);
			glTexCoord2f(0,1);
			glVertex3f(endx,endy,0);
			glTexCoord2f(0,0);
			glVertex3f(endx,endy,heigth);
			glTexCoord2f(1,0);
			glVertex3f(startx,endy,heigth);
		glEnd();
			if (orientation == VERT ){
				glNormal3f(1.0,0.0,0.0);
				applyTexture(tex);
			}
			else{
				glNormal3f(0.0,1.0,0.0);
				applyTexture(TX_DOORSIDE);
			}
		glBegin(GL_QUADS);
//right
			glTexCoord2f(1,1);
			glVertex3f(endx,starty,0);
			glTexCoord2f(0,1);
			glVertex3f(endx,endy,0);
			glTexCoord2f(0,0);
			glVertex3f(endx,endy,heigth);
			glTexCoord2f(1,0);
			glVertex3f(endx,starty,heigth);
		glEnd();
			if (orientation == VERT ){
				applyTexture(tex);
				glNormal3f(1.0,0.0,0.0);
			}
			else{
				applyTexture(TX_DOORSIDE);
				glNormal3f(0.0,1.0,0.0);
			}
		glBegin(GL_QUADS);
//left
			glTexCoord2f(1,1);
			glVertex3f(startx,starty,0);
			glTexCoord2f(0,1);
			glVertex3f(startx,endy,0);
			glTexCoord2f(0,0);
			glVertex3f(startx,endy,heigth);
			glTexCoord2f(1,0);
			glVertex3f(startx,starty,heigth);
		glEnd();
	glPopMatrix();
}
